home *** CD-ROM | disk | FTP | other *** search
/ PC go! 2008 April / PCgo 2008-04 (DVD).iso / interface / contents / demoversionen_3846 / 13664 / files / Data1.cab / viewpict.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-16  |  3.3 KB  |  110 lines

  1. /******************************************************************/
  2. /*                                                                */
  3. /*                      TurboCAD for Windows                      */
  4. /*                   Copyright (c) 1993 - 2001                    */
  5. /*             International Microcomputer Software, Inc.         */
  6. /*                            (IMSI)                              */
  7. /*                      All rights reserved.                      */
  8. /*                                                                */
  9. /******************************************************************/
  10.  
  11. // ViewPict.cpp : Defines the entry point for the DLL application.
  12. //
  13.  
  14. #include "stdafx.h"
  15. #include "ViewPict.h"
  16.  
  17. BOOL APIENTRY DllMain( HANDLE hModule, 
  18.                        DWORD  ul_reason_for_call, 
  19.                        LPVOID lpReserved
  20.                      )
  21. {
  22.     switch (ul_reason_for_call)
  23.     {
  24.         case DLL_PROCESS_ATTACH:
  25.         case DLL_THREAD_ATTACH:
  26.         case DLL_THREAD_DETACH:
  27.         case DLL_PROCESS_DETACH:
  28.             break;
  29.     }
  30.     return TRUE;
  31. }
  32.  
  33. // IN: DIB memory object
  34. // RETURNS: StdPicture object
  35. // SIDE EFFECTS: original memory object is destroyed
  36. //   StdPicture that is returned owns a DDB created from the original DIB
  37. VIEWPICT_API IDispatch* __stdcall BitmapToPicture(HGLOBAL hDIB)
  38. {
  39.     // Convert DIB into DDB for use in PICTDESC
  40.     BITMAPINFOHEADER* lpbi = (BITMAPINFOHEADER*)GlobalLock(hDIB);
  41.     WORD nNumColors = (WORD)lpbi->biClrUsed;
  42.     if (nNumColors == 0 && lpbi->biBitCount < 24)
  43.         nNumColors = 1 << lpbi->biBitCount;    /* standard size table */
  44.     WORD offBits = (WORD)lpbi->biSize + nNumColors * sizeof(RGBQUAD);
  45.     LPSTR lpBits = (LPSTR)lpbi + offBits;
  46.  
  47.     HDC hDC = GetDC(NULL);
  48.     HBITMAP hBitmap = CreateCompatibleBitmap(hDC, (WORD)lpbi->biWidth, (WORD)lpbi->biHeight);
  49.     ::SetDIBits(hDC, hBitmap, 0, (WORD)lpbi->biHeight, lpBits, (BITMAPINFO*)lpbi, DIB_RGB_COLORS);
  50.  
  51.     // Done with original DIB
  52.     GlobalUnlock(hDIB);
  53.     GlobalFree(hDIB);
  54.     hDIB = NULL;
  55.  
  56.     // Transfer ownership of DDB to StdPicture object
  57.     PICTDESC pd;
  58.     ZeroMemory(&pd, sizeof(pd));
  59.     pd.cbSizeofstruct = sizeof(pd);
  60.     pd.picType = PICTYPE_BITMAP;
  61.     pd.bmp.hbitmap = hBitmap;
  62.     pd.bmp.hpal = 0;
  63.  
  64.     IDispatch* pPicture = NULL;
  65.     HRESULT hr = OleCreatePictureIndirect(&pd, IID_IDispatch, TRUE, (void**)&pPicture);
  66.  
  67.     // Done with DC
  68.     ReleaseDC(NULL, hDC);
  69.  
  70.     if (FAILED(hr) || pPicture == NULL)
  71.     {
  72.         // Destroy failed bitmap
  73.         DeleteObject(hBitmap);
  74.         return 0;
  75.     }
  76.  
  77.     // Success
  78.     return pPicture;
  79. }
  80.  
  81. // IN: Metafile handle
  82. // RETURNS: StdPicture object
  83. // SIDE EFFECTS: original Metafile object is destroyed on error,
  84. //   otherwise, it is owned by the returned StdPicture object.
  85. VIEWPICT_API IDispatch* __stdcall MetafileToPicture(HMETAFILE hMF, long width, long height)
  86. {
  87.     // Transfer ownership of Metafile to StdPicture object
  88.     PICTDESC pd;
  89.     ZeroMemory(&pd, sizeof(pd));
  90.     pd.cbSizeofstruct = sizeof(pd);
  91.     pd.picType = PICTYPE_METAFILE;
  92.     pd.wmf.hmeta = (HMETAFILE)hMF;
  93.     pd.wmf.xExt = width;
  94.     pd.wmf.yExt = height;
  95.  
  96.     IDispatch* pPicture = NULL;
  97.     HRESULT hr = OleCreatePictureIndirect(&pd, IID_IDispatch, TRUE, (void**)&pPicture);
  98.     if (FAILED(hr) || pPicture == NULL)
  99.     {
  100.         // Destroy failed metafile
  101.         DeleteMetaFile(hMF);
  102.         return 0;
  103.     }
  104.  
  105.     // Success
  106.     return pPicture;
  107. }
  108.  
  109.  
  110.